//
//  FBPoster.m
//  HahnWinery
//
//  Created by HaiNguyenThanh on 3/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FBPoster.h"


static NSString *_attachment =
@"{\"name\":"
@"\"Politics Game\","
@"\"href\":"
@"\"http://www.facebook.com/apps/application.php?id=172238159459050\"}";


@interface FBPoster()

- (void)attemptAction;
- (id)initPostToWall:(NSString *) message;
- (void)removeSendingScreen;
- (void)showSendingScreen;

@end


@implementation FBPoster
	


- (void)attemptAction {
	[[FBRequest requestWithDelegate:self] call:@"facebook.Stream.publish" params:_params];
	[self showSendingScreen];
}


- (void)dealloc {
	[_params release];
	[self removeSendingScreen];
	/*
	if (_popAtEnd) {
		[[HomeScreenController shared].navigationController
		 popViewControllerAnimated:YES];
	}*/
	
	[super dealloc];
}


- (void)dialog:(FBDialog*)dialog didFailWithError:(NSError*)error {
	[self removeSendingScreen];
	//NSLog(@"Facebook dialog failed (%d): %@", [error code], [error localizedDescription]);
	//[HomeScreenController showCantConnectToFacebook];
	[self autorelease];
}


//
// Called when the extended permission is not granted.
//
- (void)dialogDidCancel:(FBDialog*)dialog {
	[self removeSendingScreen];
	[self autorelease];
}


//
// Called when the extended permission is granted successfully.
//
- (void)dialogDidSucceed:(FBDialog*)dialog {
	[self attemptAction];
}


- (id)initPostToWall:(NSString *) message
{
	_params = nil;
	_popAtEnd = YES;
	_sendingScreen = nil;
	_sendingText = nil;
	_successText = nil;
	
	if (self = [super init]) {
		_sendingText = @"Posting to your wall...";
		_successText = @"Your messaging has been posted to your wall successfully!";
		
		NSString *s = [NSString stringWithFormat:@"%@",message];
		
		_params = [[NSDictionary dictionaryWithObjectsAndKeys:
					s, @"message",
					_attachment, @"attachment",
					nil] retain];
		
		[self attemptAction];
	}
	
	return self;
}



+ (BOOL)isLoggedOn {
	FBSession *session = [FBSession session];
	return (session != nil && session.isConnected);
}


+ (void)PostToWall:(NSString *) message
{
	[[FBPoster alloc]initPostToWall:message];
}


- (void)removeSendingScreen {
	if (_sendingScreen != nil) {
		for (UIView *v in _sendingScreen.subviews) {
			[v removeFromSuperview];
		}
		
		[_sendingScreen removeFromSuperview];
		_sendingScreen = nil;
	}
}


- (void)request:(FBRequest*)request didFailWithError:(NSError *)error
{
	[self removeSendingScreen];
	
	if ([error code] == FBAPI_EC_PERMISSION) 
	{
		FBPermissionDialog *dialog = [[[FBPermissionDialog alloc] init] autorelease];
		dialog.delegate = self;
		dialog.permission = @"publish_stream";
		[dialog show];
	}
	else
	{
		[self autorelease];
	}
}


- (void)request:(FBRequest*)request didLoad:(id)result
{
	[self removeSendingScreen];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:_successText delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
	[alert show];
	[alert release];
	[self autorelease];
}


- (void)showSendingScreen {
	[self removeSendingScreen];
	
	// Create the sending screen.
	
	UIWindow *window = [UIApplication sharedApplication].keyWindow;
	_sendingScreen = [[UIView alloc] initWithFrame:window.bounds];
	_sendingScreen.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.82];
	[window addSubview:_sendingScreen];
	
	// Add the label.
	
	CGRect f = CGRectMake(0, 210, window.frame.size.width, 30);
	UILabel *label = [[[UILabel alloc] initWithFrame:f] autorelease];
	label.backgroundColor = [UIColor clearColor];
	label.font = [UIFont systemFontOfSize:16.0];
	label.text = _sendingText;
	label.textAlignment = UITextAlignmentCenter;
	label.textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
	[_sendingScreen addSubview:label];
	
	// Add the spinning circle.
	
	UIActivityIndicatorView *circle = [[UIActivityIndicatorView alloc]
									   initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
	circle.frame = CGRectMake(0.5 * window.frame.size.width - 0.5 * 30, 270, 30, 30);
	[circle startAnimating];
	[_sendingScreen addSubview:circle];
	[circle release];
}


@end